package com.briup.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Auther: xiongwenbin
 * @Date: 2024/7/18-07-18-下午4:28
 * @Description: org.example.com.briup.util
 */
public class jdbcUtil {
    private static DruidDataSource dataSource;
    static {
        Properties properties = new Properties();
        try {
            properties.load(ClassLoader.getSystemResourceAsStream("db.properties"));
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static Connection getConnection() throws Exception {
        return dataSource.getConnection();
    }

    public static void close(Connection conn, Statement statement, ResultSet resultSet) throws IOException {
        try{
            if(conn!=null){
                conn.close();
            }

            if(statement!=null) {
                statement.close();
            }

            if(resultSet!=null) {
                resultSet.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static <T> List<T> getSelect(String sql, Class<T> clazz) throws Exception {
        Connection conn = getConnection();
        Statement statement = conn.createStatement();
        ResultSet rs = statement.executeQuery(sql);
        ResultSetMetaData metaData = rs.getMetaData();
        T t = clazz.newInstance();
        List<T> list = new ArrayList<T>();
        Field[] fields = clazz.getDeclaredFields();
        while (rs.next()) {
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                String columnName = metaData.getColumnName(i);
                int columnType = metaData.getColumnType(i);
                // 根据列的数据类型调用相应的方法
                switch (columnType) {
                    case Types.INTEGER:
                    case Types.TINYINT:
                        for (Field field : fields) {
                            if (field.getName().equals(columnName)) {
                                field.setAccessible(true);
                                field.set(t, rs.getInt(columnName));
                            }
                        }
                        break;
                    case Types.VARCHAR:
                        for (Field field : fields) {
                            if (field.getName().equals(columnName)) {
                                field.setAccessible(true);
                                field.set(t, rs.getString(columnName));
                            }
                        }
                        break;
                    case Types.TIMESTAMP:
                        for (Field field : fields) {
                            if (field.getName().equals(columnName)) {
                                field.setAccessible(true);
                                field.set(t, rs.getTimestamp(columnName));
                            }
                        }
                        break;
                        //Types.FLOAT与columnType获得的float类型不一致 ，需要加1
                    case Types.FLOAT+1:
                        for (Field field : fields) {
                            if (field.getName().equals(columnName)) {
                                field.setAccessible(true);
                                field.set(t, rs.getFloat(columnName));
                            }
                        }
                        break;
                    default:
                        System.out.println("Unsupported data type for column: " + columnName);
                }
            }

            list.add(t);
        }
        close(conn, statement, rs);
        return list;
    }





}
